/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rbh.risk.dao.impl;

import com.rbh.risk.dao.CategoryDao;
import com.rbh.risk.entity.Category;
import com.rbh.risk.support.SessionFactorySupport;
import java.util.List;
import org.hibernate.Query;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

@Repository("categoryDao")
public class CategoryDaoImpl extends SessionFactorySupport implements CategoryDao {

    @Override
    public void insert(Category category) throws DataAccessException {
        try {
            super.getSessionFactory().getCurrentSession().save(category);
        } catch (Exception ex) {
            throw ex;
        }
    }

    @Override
    public void update(Category category) throws DataAccessException {
        try {
            super.getSessionFactory().getCurrentSession().update(category);
        } catch (Exception ex) {
            throw ex;
        }
    }

    @Override
    public void delete(Category category) throws DataAccessException {
        try {
            super.getSessionFactory().getCurrentSession().delete(category);
        } catch (Exception ex) {
            throw ex;
        }
    }

    @Override
    public Category getById(Integer categoryId) throws DataAccessException {
        Category result = null;
        try {
            result = (Category) super.getSessionFactory().getCurrentSession().get(Category.class, categoryId.shortValue());
        } catch (Exception ex) {
            throw ex;
        }
        return result;
    }

    @Override
    public List<Category> getAll() throws DataAccessException {
        List<Category> results = null;
        try {
            Query query = super.getSessionFactory().getCurrentSession().createQuery("from " + Category.class.getName());
            results = query.list();
        } catch (Exception ex) {
            throw ex;
        }
        return results;
    }
}
